package com.jsp.controller.usercontroller;

import com.jsp.entity.User;
import com.jsp.mapper.impl.BzdmMapperImpl;
import com.jsp.mapper.impl.DepartMapperImpl;
import com.jsp.mapper.impl.UserMapperImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserDetailServlet", value = "/userDetail")
public class UserDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        try {
            String yhid = request.getParameter("YHID");
            if (null == yhid || "".equals(yhid)) {
                throw new RuntimeException("用户不存在");
            }
            UserMapperImpl userMapper = new UserMapperImpl();
            User user = userMapper.findByUsername(yhid);
            if (null == user) {
                throw new RuntimeException("用户不存在");
            }
            user.setCsrq(user.getCsrq().substring(0, 4) + "-" + user.getCsrq().substring(4, 6) + "-" + user.getCsrq().substring(6, 8));
            request.setAttribute("user", user);

            DepartMapperImpl departMapper = new DepartMapperImpl();
            request.setAttribute("departList", departMapper.findAll());

            BzdmMapperImpl bzdmMapper = new BzdmMapperImpl();
            request.setAttribute("sexList", bzdmMapper.findAll());

            String type = request.getParameter("type");
            request.setAttribute("type", type);

            request.getRequestDispatcher("/detail.jsp").forward(request, response);
        }catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
